package gupao.concurrency.lock;

import gupao.concurrency.threads.Utils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SynchronizedUnInterruptable {

    public static synchronized void syncStaticMethod(){
        log.info("in synchronized method ");
        Utils.waitNanos(10 * 1000 * 1000 * 1000);
    }

    public static void main(String[] args) {
        Runnable task = () -> {
            log.info("acquire synchronized lock");
            try{
                syncStaticMethod();
                log.info("task finished.");
            } catch (Exception e){
                if( e instanceof InterruptedException){
                    e.printStackTrace();
                }
            }

        };
        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);
        t1.start();
        t2.start();

        log.info("interrupt thread {}", t2.getName());
        t2.interrupt();
        log.info("interrupt thread {}", t2.getName());
        t2.interrupt();

        while(Thread.activeCount() > 1){
            Thread.yield();
        }
        log.info("completed");
    }
}
